package com.buddy.learn.javasource._collection;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 当我们需要在线程不安全场景下使用 List 时，建议使用 CopyOnWriteArrayList，
 * CopyOnWriteArrayList 通过锁 + 数组拷贝 + volatile 之间的相互配合，
 * 实现了 List 的线程安全
 * @author Buddy
 * @date 2021/5/7
 */
public class _CopyOnWriteArrayList {

    public static void main(String[] args) {
        CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList();
        ArrayList arrayList = new ArrayList();
        arrayList.add(null);
        arrayList.add(1, null);
        copyOnWriteArrayList.add("A");
        copyOnWriteArrayList.add(null);
        copyOnWriteArrayList.add(4, null);
        copyOnWriteArrayList.remove(3);
        copyOnWriteArrayList.removeAll(arrayList);
        copyOnWriteArrayList.indexOf(null);
    }
}
